package com.report.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.URLUtil;
import cn.zhxu.bs.BeanSearcher;
import com.alipay.api.domain.MemberRule;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.report.dto.request.BrowseReq;
import com.report.dto.request.InsightReq;
import com.report.dto.request.ReportReq;
import com.report.dto.request.SpecialReq;
import com.report.dto.response.*;
import com.report.pdf.PdfUtils;
import com.report.service.*;
import com.zmh.framework.exception.ResultException;
import com.zmh.framework.sys.dto.request.common.PageResponseDTO;
import com.zmh.framework.util.Result;
import lombok.AllArgsConstructor;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Base64;
import java.util.List;

/**
 * @author summer
 * @date 2024-07-03 19:58
 * 二级页面跳转
 */
@RestController
@RequestMapping(value = "/web")
@AllArgsConstructor
@CrossOrigin
public class WebItemController {

    private final IReportService reportService;

    private final IBrowseService browseService;

    /**
     * 查询报告二级页面
     * @param id
     * @return
     */
    @GetMapping(value = "/findReportById/{id}")
    public Result<ReportRes> findReportById(@PathVariable Integer id, HttpServletRequest request)  {
        ReportRes reportRes= reportService.findById(id);
        if(StpUtil.isLogin()){
            BrowseReq browseReq = new BrowseReq();
            browseReq.setReportId(id);
            browseReq.setMemberId(Convert.convert(Integer.class, StpUtil.getLoginId()));
            browseService.saveBrowse(browseReq);
        }else{
          //  browseService.isReadNum(request);
         }
        return Result.successResult(reportRes);
    }

    /**
     * 最新报告
     * @param reportReq
     * @return
     */
    @PostMapping(value = "/newReportList")
    public Result<Page<ReportRes>> newReportList(@RequestBody ReportReq reportReq)  {
        Page<ReportRes> page=reportService.newReportList(reportReq);
        return Result.successResult(page);
    }

    private final IReportFileService reportFileService;

    /**
     * 查询专题详情页面
     * @param req
     * @return
     */
     @GetMapping("/findSpecialById/{id}")
    public Result<SpecialRes> findSpecialById(@PathVariable Integer id) {
         SpecialRes special=reportFileService.findSpecialById(id);
         return  Result.successResult(special);
    }

    @PostMapping(value = "/findReportList")
    public Result<Page<SubjectRes>> findReportList(@RequestBody SpecialReq req) {

         return Result.successResult(reportService.findReportList(req));

    }

    /**
     * 查询专题下的合集
     * @param insightReq
     * @return
     */
    @PostMapping(value = "/findSpecialAndReportList")
    public Result<Page<ReportRes>> findSpecialAndReportList(@RequestBody InsightReq insightReq) {
       return Result.successResult(reportFileService.findSpecialAndReportList(insightReq));
    }

    /**
     * VIP下载专题报告合集
     * @param id
     * @param response
     */
    @GetMapping(value = "/downloadVipReportFile/{id}")
    public void downloadVipReportFile(@PathVariable Integer id, HttpServletResponse response) {
        reportFileService.downloadVipReportFile(id,response);
    }


    @GetMapping(value = "/vipReport/{id}")
    public Result<List<ReportRes>> downloadVipReportFile(@PathVariable Integer id) {

        return Result.successResult(reportFileService.vipReport(id));
    }


    private final InsightService insightService;

    /**
     * 查询洞察详情
     * @param id
     * @return
     */
    @GetMapping("/findInsightById/{id}")
    public Result<InsightRes> findInsightById(@PathVariable Integer id) {
        InsightRes insightRes=insightService.findInsightById(id);
         return Result.successResult(insightRes);
    }


    @PostMapping(value = "/insightHotspot")
    public Result<Page<InsightRes>> insightHotspot(@RequestBody PageResponseDTO pageResponseDTO){
       Page<InsightRes> page=  insightService.insightHotspot(pageResponseDTO);
        return Result.successResult(page);
    }

    @GetMapping(value = "/saveReadInsight/{id}")
    public Result<String> saveReadInsight(@PathVariable Integer id){
        insightService.saveReadInsight(id);
        return Result.successResult();
    }


    /**
     * 上下篇专题数据
     * @return
     */
    @GetMapping("/preNextInsight/{id}")
    public Result<List<InsightWebRes>> preNextInsight(@PathVariable Integer id) {
        List<InsightWebRes> reportRes=insightService.preNextInsight(id);
        return Result.successResult(reportRes);
    }


    private final BeanSearcher beanSearcher;

    @GetMapping(value = "/memberRule")
    public Result<List<MemberRuleRes>> lists(){
        List<MemberRuleRes> memberRules = beanSearcher.searchList(MemberRuleRes.class);
        return Result.successResult(memberRules);
    }

}
